package com.zhitian.fortest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class Mongo {

	public static void main(String[] args) {
		try {
			MongoClient mongoClient = new MongoClient("localhost",27017);
			 
			MongoDatabase db = mongoClient.getDatabase("test");
			System.out.println(db.getName());
			
			//mongoDatabase.createCollection("fortest");
			
			MongoCollection<Document> collection = db.getCollection("fortest");
			
			/*Document document = new Document("title","MongoDB").
			append("description", "name").
			append("time", new Timestamp(System.currentTimeMillis()));
			
			List<Document> documents = new ArrayList<>();
			documents.add(document);
			collection.insertMany(documents);
			System.out.println("insert ok");*/
			
			collection.updateMany(Filters.eq("title","oop"), new Document("$set",new Document("description","oop")));
			
			FindIterable<Document> findIterable = collection.find();
			MongoCursor<Document> cursor = findIterable.iterator();
			while(cursor.hasNext()) {
				System.out.println(cursor.next().get("time"));
				
				System.out.println(cursor.getServerAddress());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
